home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / ts_fcast.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  126 lines

  1. ; $Id: ts_fcast.pro,v 1.8 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1995-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       TS_FCAST
  8. ;
  9. ; PURPOSE:
  10. ;       This function computes future or past values of a stationary time-
  11. ;       series (X) using a Pth order autoregressive model. The result is an
  12. ;       Nvalues-element vector whose type is identical to X.
  13. ;
  14. ; CATEGORY:
  15. ;       Statistics.
  16. ;
  17. ; CALLING SEQUENCE:
  18. ;       Result = TS_FCAST(X, P, Nvalues)
  19. ;
  20. ; INPUTS:
  21. ;       X:    An n-element vector of type float or double containing time-
  22. ;             series samples.
  23. ;
  24. ;       P:    A scalar of type integer or long integer that specifies the
  25. ;             number of actual time-series values to be used in the forecast. 
  26. ;             In general, a larger number of values results in a more accurate
  27. ;             result.
  28. ;
  29. ; Nvalues:    A scalar of type integer or long integer that specifies the
  30. ;             number of future or past values to be computed.
  31. ;
  32. ; KEYWORD PARAMETERS:
  33. ;   BACKCAST: If set to a non-zero value, "backcasts" (backward-forecasts)
  34. ;             are computed.
  35. ;
  36. ;     DOUBLE: If set to a non-zero value, computations are done in
  37. ;             double precision arithmetic.
  38. ;
  39. ; EXAMPLE:
  40. ;       Define an n-element vector of time-series samples.
  41. ;         x = [6.63, 6.59, 6.46, 6.49, 6.45, 6.41, 6.38, 6.26, 6.09, 5.99, $
  42. ;              5.92, 5.93, 5.83, 5.82, 5.95, 5.91, 5.81, 5.64, 5.51, 5.31, $
  43. ;              5.36, 5.17, 5.07, 4.97, 5.00, 5.01, 4.85, 4.79, 4.73, 4.76]
  44. ;
  45. ;       Compute five future and five past values of the time-series using a 
  46. ;       10th order autoregressive model.
  47. ;         resultF = ts_fcast(x, 10, 5)
  48. ;         resultB = ts_fcast(x, 10, 5, /backcast)
  49. ;
  50. ;       The forecast (resultF) should be:
  51. ;         [4.65870, 4.58380, 4.50030, 4.48828, 4.46971] 
  52. ;       The backcast (resultB) should be:
  53. ;         [6.94862, 6.91103, 6.86297, 6.77826, 6.70282]
  54. ;
  55. ; REFERENCE:
  56. ;       The Analysis of Time Series, An Introduction (Fourth Edition)
  57. ;       Chapman and Hall
  58. ;       ISBN 0-412-31820-2
  59. ;
  60. ; MODIFICATION HISTORY:
  61. ;       Written by:  GGS, RSI, November 1994
  62. ;       Modified:    GGS, RSI, January 1996
  63. ;                    Added DOUBLE keyword.               
  64. ;                    Added BACKCAST keyword.
  65. ;       Modified:    GGS, RSI, June 1996
  66. ;                    Modified keyword checking and use of double precision.
  67. ;-
  68.  
  69. FUNCTION TS_Fcast, x, p, Nvalues, Backcast = Backcast, Double = Double, $
  70.                                   Reflect = Reflect
  71.  
  72.   ;This function uses the last P elements [Xn-1, Xn-2, ... , Xn-p]
  73.   ;of the time-series [x0, x1, ... , xn-1] to compute the forecast. 
  74.   ;More coefficients correspond to more past time-series data used 
  75.   ;to make the forecast.
  76.  
  77.   ;REFLECT keyword is not currently supported.
  78.  
  79.   ON_ERROR, 2
  80.  
  81.   Nvalues = LONG(Nvalues)
  82.  
  83.   if Nvalues le 0 then $
  84.     MESSAGE, "Nvalues must be a scalar greater than 0."
  85.  
  86.   TypeX = SIZE(x)
  87.   nX = TypeX[TypeX[0]+2]
  88.  
  89.   if p lt 2 or p gt nX-1 then $
  90.     MESSAGE, "p must be a scalar in the interval: [2, N_ELEMENTS(x)-1]."
  91.  
  92.   ;If the DOUBLE keyword is not set then the internal precision and
  93.   ;result are identical to the type of input.
  94.   if N_ELEMENTS(Double) eq 0 then Double = (TypeX[TypeX[0]+1] eq 5)
  95.  
  96.   ;Reverse the time-series for backcasting.
  97.   if KEYWORD_SET(Backcast) ne 0 then x = ROTATE(x,5)
  98.  
  99.   ;The last p elements of the time-series.
  100.   Data = ROTATE(x[nX-LONG(p):nX-1],5)
  101.  
  102.   if Double ne 0 then Fcast = DBLARR(Nvalues) $
  103.   else Fcast = FLTARR(Nvalues)
  104.  
  105.   ;Compute coefficients.
  106.     ARcoeff = TS_COEF(x, LONG(p), Double = Double)
  107.  
  108.   for j = 0, Nvalues-1 do begin
  109.     ;;yn = total(Data * ARcoeff, Double = Double)
  110.     ;Data = [yn, Data[0:Nvalues-2]]
  111.     Data = [TOTAL(Data*ARcoeff, Double = Double), Data[0:LONG(p)-2]]
  112.     ;;Data = [yn, Data[0:long(p)-2]]
  113.     Fcast[j] = Data[0]
  114.   endfor
  115.  
  116.   if KEYWORD_SET(Backcast) ne 0 then begin
  117.     ;Restore the order of the time-series if backcasting.
  118.     x = ROTATE(x,5)
  119.     RETURN, ROTATE(Fcast,5)
  120.   endif else RETURN, Fcast
  121.  
  122. END
  123.  
  124.  
  125.  
  126.